﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

using ExcelDataReader;
using System.Text.RegularExpressions;

namespace CalHelp
{
    public partial class Form1 : Form
    {
        DataTable dtmx明细;
        public Form1()
        {
            InitializeComponent();
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(this.dtmx明细==null)
                this.dtmx明细 = getDataTable();
            if (this.dtmx明细 != null)
            {
                this.Calculate("一. 1k(含980)-2k", "售卡金额>=980 and 售卡金额<=2000 and 业绩类型<>'合作卡业绩'");
                this.Calculate("二. 2k-6k", "售卡金额>2000 and 售卡金额<=6000 and 业绩类型<>'合作卡业绩'");
                this.Calculate("三. 6k以上", "售卡金额>6000 and 业绩类型<>'合作卡业绩'");
                this.Calculate("四. 合作项目", "业绩类型 like '合作卡%'");
                this.Calculate("六. 储值股金", "业绩类型 like '储值卡%' or 业绩类型 like '股东卡%'");
                this.Calculate("七. 赠券", this.getCondition赠券());
                this.Calculate("八. 赠项目", "卡类型 = '项目券'");
                this.Calculate("九. 旧次数卡", "卡类型 = '旧次数卡'");
            }
            else {
                MessageBox.Show("没有数据,无法计算", "错误");
            }
        }

        private string getCondition赠券() 
        {
            string[] zsqlx = this.get赠送券卡类型();
            string result = "";
            foreach (string x in zsqlx) {
                if (x.Contains("%")) {
                    result = result + "卡类型 like '" + x +"'";
                }
                else
                    result = result + "卡类型='" + x + "'";
                if (x != zsqlx.Last()) {
                    result = result + " or ";
                }
            }
            return result;
        }

        private DataTable getDataTable() 
        {
            try
            {
                string fn = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "会员卡负债明细表.xls");
                FileStream fs = File.Open(fn, FileMode.Open, FileAccess.Read);
                IExcelDataReader exreader = ExcelReaderFactory.CreateReader(fs);
                DataSet ds = exreader.AsDataSet(new ExcelDataSetConfiguration { ConfigureDataTable = _ => new ExcelDataTableConfiguration { UseHeaderRow = true } });
                var dt = ds.Tables[0];
                return dt;
            }
            catch (Exception ex) {
                MessageBox.Show("打开文件失败：" + ex.Message, "错误!");
                return null;
            }
        }

        private string[] get赠送券卡类型() {
            return System.IO.File.ReadAllLines("赠送券卡类型.txt");
        }

        private void btn1k2k_Click(object sender, EventArgs e)
        {
            
        }

        private void Calculate(string caption,string condition) {
            DataRow[] rows = this.dtmx明细.Select(condition);
            //this.textBox1.Lines = (rows.Select(x=>x["剩余项目"] as string).ToArray());
            int itemLeftNumber = this.计算剩余项目数(rows);
            int itemLeftPeople = this.计算人数(rows);
            decimal leftMoney = this.计算余额(rows);
            string resultStrings = $"\r\n {caption}: \r\n 剩余人头\t 项目余次\t 项目总价\t 项目均价\r\n {itemLeftPeople}\t{itemLeftNumber}\t{leftMoney}\t{ (itemLeftNumber==0?0:Math.Round(leftMoney / itemLeftNumber, 2))}\r\n";
            this.textBox1.AppendText(resultStrings);

        }

        private decimal 计算余额(DataRow[] rows) {
            
            decimal result =  rows.Where(x=>x["余额"]!=null)
                .Sum(x => decimal.Parse(x["余额"].ToString()));
            return result ;
        }

        private int 计算剩余项目数(DataRow[] rows) 
        {
            int result = rows.Sum(x => this.get剩余项目数(x["剩余项目"] as string));
            return result;
        }

        private int 计算人数(DataRow[] rows) {
            int result = rows.Select(x => x["会员编号"]).Distinct().Count();
            return result;
        }

        private int get剩余项目数(string bracedNumbers) {

            
            Regex regx = new Regex(@"(?<=\()\d+(?=\))");
            var matches = regx.Matches(bracedNumbers);
            int result = 0;
            Match[] arr=new Match[matches.Count];
            if (matches.Count > 0) {
                matches.CopyTo(arr,0);
                result = arr.Sum(x => int.Parse(x.Value));
            }
            
            return result;
        }


        private void btn2k6k_Click(object sender, EventArgs e)
        {

        }

        private void btn6kabove_Click(object sender, EventArgs e)
        {

        }

        private void btncolaborate_Click(object sender, EventArgs e)
        {

        }

        private void btnSaveValue_Click(object sender, EventArgs e)
        {

        }

        private void btnCoupon_Click(object sender, EventArgs e)
        {

        }

        private void btnGiftItem_Click(object sender, EventArgs e)
        {

        }

        private void btnOldCard_Click(object sender, EventArgs e)
        {

        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            this.textBox1.Clear();
        }
    }
}
